package com.base.organization;

import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.infomanage.exception.DaoException;
import com.jiuqi.webtree.TreeNode;
import com.jiuqi.webtree.TreeNodeList;
import com.jiuqi.webtree.WebTreeInfo;
import com.jiuqi.webtree.WebTreeIntf;
import com.jiuqi.webtree.util.StringUtils;

public class OrganizationTree implements WebTreeIntf{

	public static final int ORGANIZATION_ROOT_NODE = -1;
	private String ORGANIZATION_ICON_PATH = "";
	private OrganizationDAO organizationDAO = null;
	
	public OrganizationTree(HttpServletRequest request){
		ORGANIZATION_ICON_PATH = request.getSession().getServletContext().getRealPath("/img/organization.png");
		organizationDAO = new OrganizationDAO();
	}
	@Override
	public boolean delNode(String arg0, boolean arg1, String arg2) {
		return false;
	}

	@Override
	public String getErrorMessage() {
		return null;
	}

	@Override
	public String[] getNodePaths(String arg0) {
		return null;
	}

	@Override
	public TreeNodeList getNodes(String parentId, int arg1, WebTreeInfo arg2) {
		TreeNodeList childNodes = new TreeNodeList(1, 1);
		try {
			if(StringUtils.isEmpty(parentId)){
				TreeNode treeNode = new TreeNode(String.valueOf(ORGANIZATION_ROOT_NODE), "所有组织机构", true, null, false);
				treeNode.setNodeIconPath(ORGANIZATION_ICON_PATH);
				treeNode.setOpenNodePath(ORGANIZATION_ICON_PATH);
				treeNode.setSelectedNodePath(ORGANIZATION_ICON_PATH);
				childNodes.addNode(treeNode);
				return childNodes;
			}
			List<Organization> list = organizationDAO.findChildren(Integer.parseInt(parentId));
			for(Organization org : list){
				TreeNode treeNode = new TreeNode(String.valueOf(org.getOrganizationId()), org.getShortName(), true, null, false);
//				Hashtable<String, String> attribtes = new Hashtable<String, String>();
//				attribtes.put("fullTitle", org.getFullName());
//				treeNode.setAttributes(attribtes);
				treeNode.setNodeIconPath(ORGANIZATION_ICON_PATH);
				treeNode.setOpenNodePath(ORGANIZATION_ICON_PATH);
				treeNode.setSelectedNodePath(ORGANIZATION_ICON_PATH);
				childNodes.addNode(treeNode);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return childNodes;
	}

	@Override
	public boolean insertNode(String arg0, String arg1, Hashtable arg2) {
		return false;
	}

}
